using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using YayiMgr.Ability.CommonApp;
using YayiMgr.Ability.Docking.Commom;
using YayiMgr.Ability.Docking.DentalProceduresApp;
using YayiMgr.Ability.Docking.DentalProceduresApp.Dto;
using YayiMgr.Domain.BasicData;
using Youshow.Ace.Ability;
using Youshow.Ace.Domain.Repository;

namespace YayiMgr.Ability.DentalProceduresApp
{
    public class DentalProceduresServicer: AbilityServicer, IDentalProceduresServicer
    {
        public IRepository<DentalProcedures> DpRepo { get; set; }

        public async Task<QueryListDto<DentalProceduresDto>> GatAllDentalProcedures()
        {
            List<DentalProcedures> dentals = await DpRepo.GetListAsync();
            List<DentalProceduresDto> dtos = ModelMapper.Map<List<DentalProcedures>,List<DentalProceduresDto>>(dentals);
            QueryListDto<DentalProceduresDto> listDto = QueryListDto<DentalProceduresDto>.Create(dtos, dtos.Count,1,dtos.Count());
            return listDto;
        }

        public async Task<QueryListDto<DentalProceduresDto>> GatDentalProcedures(DentalProceduresQueryDto dto)
        {
            IQueryable<DentalProcedures> query = DpRepo.AsQueryable();
            if(dto.CompanyId!=null)
                query = query.Where(d=> d.CompanyId==dto.CompanyId);
            if(!string.IsNullOrWhiteSpace(dto.Name))
                query = query.Where(d=>d.Name.Contains(dto.Name));
            if(dto.IsDiscount!=null)
                query = query.Where(d=> d.IsDiscount== dto.IsDiscount);


            List<DentalProcedures> dentals = await query.OrderBy(d=> d.CompanyId).ThenBy(d=> d.Name)
                                                .Skip((dto.PageIndex-1)*dto.PageSize).Take(dto.PageSize).ToListAsync();
            List<DentalProceduresDto> dtos = ModelMapper.Map<List<DentalProcedures>,List<DentalProceduresDto>>(dentals);
            int total = await query.CountAsync();
            QueryListDto<DentalProceduresDto> listDto = QueryListDto<DentalProceduresDto>.Create(dtos, total,dto.PageIndex,dto.PageSize);
            return listDto;
        }

        public async Task<ResponseDto<DentalProceduresDto>> GetDentalProceduresById(Guid id)
        {
            DentalProcedures item = await DpRepo.GetAsync(id);
            if(item==null)
                return ResponseDto<DentalProceduresDto>.CreateFail("无数据");
            DentalProceduresDto dto2 = ModelMapper.Map<DentalProcedures, DentalProceduresDto>(item);
            return ResponseDto<DentalProceduresDto>.CreateSuccess(dto2);
        }

        public async Task<ResponseDto<DentalProceduresDto>> AddDentalProcedures(DentalProceduresCreateDto dto)
        {
            DentalProcedures item = ModelMapper.Map<DentalProceduresCreateDto, DentalProcedures>(dto);
            item = await DpRepo.InsertAsync(item);
            DentalProceduresDto dto2 = ModelMapper.Map<DentalProcedures, DentalProceduresDto>(item);
            return ResponseDto<DentalProceduresDto>.CreateSuccess(dto2);
        }

        public async Task<ResponseDto<DentalProceduresDto>> UpdateDentalProcedures(Guid id, DentalProceduresUpdateDto dto)
        {
            DentalProcedures item = await DpRepo.GetAsync(id);
            if(item==null)
                return ResponseDto<DentalProceduresDto>.CreateFail("无数据");
                
            item.UpdateEntity<DentalProcedures>(ModelMapper.Map<DentalProceduresUpdateDto,DentalProcedures>(dto), new List<string>{"Id"});
            DentalProceduresDto dto2 = ModelMapper.Map<DentalProcedures, DentalProceduresDto>(item);
            return ResponseDto<DentalProceduresDto>.CreateSuccess(dto2);
        }

        public async Task<ResponseDto<Object>> DeleteDentalProcedures(Guid id)
        {
            DentalProcedures item = await DpRepo.GetAsync(id);
            if(item==null)
                return ResponseDto<Object>.CreateFail("无数据");
            
            await DpRepo.DeleteAsync(item);
            return ResponseDto<Object>.CreateSuccess(new {});
        }
    }
}